package sapling.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 云平台后台登录用户的信息。存储用户登录名，密码等。
 * <p>
 * Created by ever on 2016/10/9.
 */
@Entity
@Getter
@Setter
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long user_id;
    @Column(unique = true)
    private String username;

    @JsonIgnore
    private String password;
    @JsonIgnore
    private String role="0";
    @JsonIgnore
    private String tooken;
    @JsonIgnore
    private String headimg;//用户头像
    @JsonIgnore
    private String nickname;
    @JsonIgnore
    private String APPID;

    @ManyToOne
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @JsonIgnore
    private Company company; //每个用户所属公司

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_role", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "role_id")})
    private List<Role> roleList;

    @Transient
    @JsonIgnore
    public Set<String> getRolesName() {
        return getRoleList().stream()
                .map(Role::getName)
                .collect(Collectors.toSet());
    }
}
